package com.puhui.lib.rxjava

import androidx.lifecycle.LifecycleOwner
import com.trello.rxlifecycle3.android.ActivityEvent
import com.trello.rxlifecycle3.android.FragmentEvent
import com.trello.rxlifecycle3.components.support.RxAppCompatActivity
import com.trello.rxlifecycle3.components.support.RxFragment
import io.reactivex.ObservableTransformer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

@Suppress("all")
object RxUtils {
    /**
     * 1、Kotlin泛型报Unchecked cast: Any to T警告
     *      原因：泛型转换校验警告
     *      解决方案：Java中在方法上添加注解@SuppressWarnings("unchecked")，在kotlin中在方法上添加注解@Suppress("UNCHECKED_CAST")

     *  2、过时api调用报'TYPE_WIFI: Int' is deprecated. Deprecated in Java警告
     *      解决方案：Java中在方法上添加注解@SuppressWarnings("deprecation")，kotlin中在方法上添加注解@Suppress("DEPRECATION")

     *   3、无调用方法或类Class "MainApplication" is never used警告
     *      解决方案：Java中在方法或类上添加注解@SuppressWarnings("unused")，kotlin中在方法或类上添加注解@Suppress("unused")

     *  4、Accessing internal APIs via reflection is not supported and may not work on all devices or in the future警告
     *      原因分析：当使用特殊机型私有api时报该警告，如MIUI系Class.forName("android.view.MiuiWindowManager\$LayoutParams")
     *      解决方案：在方法上添加注解@SuppressLint("PrivateApi")
     */

    @JvmStatic
    fun <T, A : LifecycleOwner> bindLifeCycle(uiView: A): ObservableTransformer<T, T> {
        when (uiView) {
            is RxAppCompatActivity -> {
                return ObservableTransformer<T, T> { upstream ->
                    upstream.compose(applyThread())
                        .compose(uiView.bindUntilEvent(ActivityEvent.DESTROY))
                }
            }
            is RxFragment -> {
                return ObservableTransformer<T, T> { upstream ->
                    upstream.compose(applyThread())
                        .compose(uiView.bindUntilEvent(FragmentEvent.DESTROY))
                }
            }
            else -> {  //uiView.lifecycle.currentState
                return ObservableTransformer<T, T> { upstream ->
                    upstream.compose(applyThread())
//                        .compose(MyLifecycleTransformer.bindUntilEvent(, Lifecycle.Event.ON_DESTROY))
                }
            }
        }
    }

//    @JvmStatic
//    fun <T, A : RxAppCompatActivity> bindLifeCycle(activity: A): ObservableTransformer<T, T> {
//        return ObservableTransformer<T, T> { upstream ->
//            upstream.compose(applyThread())
//                .compose(activity.bindUntilEvent(ActivityEvent.DESTROY))
//        }
//    }

//    @JvmStatic
//    fun <T, F : RxFragment> bindLifeCycle(fragment: RxFragment): ObservableTransformer<T, T> {
//        return ObservableTransformer<T, T> { upstream ->
//            upstream.compose(applyThread())
//                .compose(fragment.bindUntilEvent(FragmentEvent.DESTROY))
//        }
//    }

    /**
     * lambda之前效果
     * fun <T, F : RxFragment> bindLifeCycle(fragment: RxFragment): ObservableTransformer<T, T> {
     *   return object : ObservableTransformer<T, T> {
     *       override fun apply(upstream: Observable<T>): ObservableSource<T> {
     *          return upstream.compose(applyThread())
     *              .compose(fragment.bindUntilEvent(FragmentEvent.DESTROY))
     *      }
     *  }
    }*/
    @JvmStatic
    fun <T> applyThread(): ObservableTransformer<T, T> {
        return ObservableTransformer<T, T> { upstream ->
            upstream.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
        }
    }
}